package com.gexiaobao.pigeon.ui.adapter

import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.EditBloodInfoBean
import com.gexiaobao.pigeon.databinding.ItemElderFourBinding

/**
 * @Author      : hxw
 * @Date        : 2024/9/14 11:22
 * @Describe    :
 */
class AdapterFourGeneration : BaseQuickAdapter<EditBloodInfoBean, DataBindBaseViewHolder>(R.layout.item_elder_four) {

    override fun convert(holder: DataBindBaseViewHolder, item: EditBloodInfoBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemElderFourBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

//        val llParent = holder.getView<LinearLayout>(R.id.llParentLayout)
        holder.setGone(R.id.ivFourItemAddData, item.ringId.isNotEmpty())
//        if (holder.layoutPosition != 1) {
//            llParent.isFocusable = !item.isVisible
//            llParent.isClickable = !item.isVisible
//        } else {
//            llParent.isFocusable = true
//            llParent.isClickable = true
//        }

        val sexIcon = if (holder.layoutPosition % 2 == 0) {//偶数
            R.mipmap.icon_male
        } else {
            R.mipmap.icon_female
        }
        holder.setImageResource(R.id.ivFourItemSex, sexIcon)

        // 动态设置边距
        val layoutParams = holder.itemView.layoutParams as ViewGroup.MarginLayoutParams
        if (holder.layoutPosition % 2 == 0) {
            layoutParams.setMargins(0, 12, 0, 0)
        } else {
            layoutParams.setMargins(0, 0, 0, 0)
        }
        holder.itemView.layoutParams = layoutParams
    }

    // 设置是否可添加数据
    fun canAddItem(position: Int, isCan: Boolean) {
        if (position in data.indices) {
            data[position].setIsClick(isCan)
            notifyItemChanged(position) // 通知更新该项
        }
    }

    // 设置是否可点击
    fun canClick(position: Int, isClick: Boolean) {
        if (position in data.indices) {
            data[position].setIsClick(isClick)
            notifyItemChanged(position)
        }
    }

    // 清空某一条数据
    fun clearItem(position: Int) {
        if (position in data.indices) {
            data[position].clear() // 清空对象的所有值
            notifyItemChanged(position) // 通知更新该项
        }
    }

    // 更新单个 item 的方法
    fun updateItem(position: Int, dataBean: EditBloodInfoBean?) {
        if (position >= 0 && position < data.size) {
            val item: EditBloodInfoBean = data[position]
            if (dataBean != null) {
                item.publicIs = dataBean.publicIs
                item.country = dataBean.country
                item.countryEn = dataBean.countryEn
                item.featherColor = dataBean.featherColor
                item.ringId = dataBean.ringId
                item.name = dataBean.name
                item.sex = dataBean.sex
                item.eye = dataBean.eye
                item.gradesDesc = dataBean.gradesDesc
                item.title = dataBean.title
                item.level = dataBean.level
                item.levelInfo = dataBean.levelInfo
            }
            notifyItemChanged(position)
        }
    }
}